require "common"

--数字字符串排序
function sort_str( num )
	local out = ""
	local st = {}
	local len = string.len( num )
	if len%2 ~= 0 then
		return nil
	end

	for i=1,len,2 do
		c = string.sub(num,i,i+1)
		if tonumber(c) > 15 or tonumber(c) <1 then return nil end
		if not in_table( c, st ) then
			table.insert( st, c )
		else
			return nil
		end
	end
	table.sort(st)
	for i=1,table.getn( st ) do
		out = out..st[i]
	end
	return out
end

function in_table ( e, t )
	for _,v in pairs(t) do
		if (v==e) then return true end
	end
	return false
end
--====================计算连续自然码的个数=================================
function ComputeSeriesNum(djStr)
	local str = {}
	for i = 1, string.len(djStr), 2 do
		str[(i+1)/2] = string.sub(djStr,i,i+1)
	end
	nMax = 1
	nTemp = 1
	for i = 1, 4 do
		if tonumber(str[i+1]) == tonumber(str[i]) + 1 then
			nTemp = nTemp + 1
		else
			if nMax < nTemp then
				nMax = nTemp
				nTemp = 1
			end
		end
	end
	if nMax < nTemp then
		nMax = nTemp
	end
	return nMax
end

---======================单复式计算=========================================
function Compute155(butType,xxStr,djStr)
	local bullStr = {}
	local queryStr = {}
	local result = {0,0,0}
	local bingoNum = 0
	local nSeriesNum = 1

	for i = 1, string.len(djStr), 2 do
		bullStr[(i+1)/2] = string.sub(djStr,i,i+1)
	end
	nSeriesNum =  ComputeSeriesNum(djStr)
	if butType == '1' or butType == '2' then
		xxItem = Split(xxStr,"xx")
		for i=1, table.getn(xxItem) do
			queryStr[i] = {}
			for j=1,string.len(xxItem[i]), 2 do
				queryStr[i][(j+1)/2] = string.sub(xxItem[i],j,j+1)
			end
		end
		for i=1, table.getn(xxItem) do
			for _,k in ipairs(bullStr) do
				for _,v in ipairs(queryStr[i]) do
					if k == v then
						bingoNum = bingoNum + 1
					end
				end
			end
			if bingoNum == 5 and nSeriesNum > 3 then
				result[1] = result[1] + 1
				bingoNum = 0
			elseif bingoNum == 5 and nSeriesNum < 4 then
				result[2] = result[2] + 1
				bingoNum = 0
			elseif bingoNum == 4 then
				result[3] = result[3] + 1
				bingoNum = 0
			end
		end
	elseif butType == '3' or butType == '4' then
		for i = 1, string.len(xxStr), 2 do
			queryStr[(i+1)/2] = string.sub(xxStr,i,i+1)
		end
		for _,k in ipairs(bullStr) do
			for _,v in ipairs(queryStr) do
				if k == v then
					bingoNum = bingoNum + 1
				end
			end
		end
		if bingoNum == 5 and nSeriesNum > 3 then
			result[1] = result[1] + 1
			result[3] = (table.getn(queryStr) - 5) * 5
		elseif bingoNum == 5 and nSeriesNum < 4 then
			result[2] = result[2] + 1
			result[3] = (table.getn(queryStr) - 5) * 5
		elseif bingoNum == 4 then
			result[3] = result[3] + 1
		end

	end

	return result--[1]..","..result[2]..","..result[3]

end
--=====================15选5中奖计算=================
function Compute155DT(xxStr,djStr)
	local queryStr = {}
	local queryD = {}
	local queryT = {}
	local bullStr = {}
	local nSeriesNum = 1
	local bingoD = 0
	local bingoT = 0
	local result = {0,0,0}
	nSeriesNum = ComputeSeriesNum(djStr)
	queryStr = Split(xxStr,"x")
	for i = 1, string.len(djStr), 2 do
		bullStr[(i+1)/2] = string.sub(djStr,i,i+1)
	end

	for i = 1, string.len(queryStr[1]), 2 do
		queryD[(i+1)/2] = string.sub(queryStr[1],i,i+1)
	end

	for i = 1, string.len(queryStr[2]), 2 do
		queryT[(i+1)/2] = string.sub(queryStr[2],i,i+1)
	end

	for _,k in ipairs(queryD) do
		for _,v in ipairs(bullStr) do
			if k == v then
				bingoD = bingoD + 1
			end
		end
	end

	for _,k in ipairs(queryT) do
		for _,v in ipairs(bullStr) do
			if k == v then
				bingoT = bingoT + 1
			end
		end
	end

	totalBingoNum = bingoD + bingoT
	dNum = table.getn(queryD)
	tNum = table.getn(queryT)
	if totalBingoNum == 5 then
		if bingoD == dNum then
			if nSeriesNum > 3 then
				result[1] = 1
			else
				result[2] = 1
			end
			result[3] = combination(bingoT,(4-bingoD)) * (dNum + tNum - 5)
		elseif bingoD == dNum - 1 then
			result[3] = combination(bingoT,(5 - dNum))
		end
	elseif totalBingoNum == 4 then
		if bingoD == dNum then
			result[3] = tNum - bingoT
		elseif bingoD == dNum - 1 then
			result[3] = 1
		end

	end
	return result
end
--返回注数,倍投数 , 投注串, 指令串
function random_155( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	--单式机选
	if butType == '1' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or not butNum or butNum > 5 or butNum < 1 or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		for i=1,butNum do
			local nums = sel_dist_num(5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
			table.sort(nums)
			zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])..format_num(nums[5])
			if i<butNum then zlStr = zlStr.."**" end
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_155('1', zlStr)

	end

	return butNum, butPower, butStr, zlStr


end


--返回注数,倍投数,投注串
function check_155( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#", "")


	--单式直选 1#052#单式号码1**单式号码2…#倍数  052 = 2,0,0,1
	if  butType == '1' then
		if itemLen > 2 or itemLen < 1  or (itemList[2] and string.find(itemList[2],"%D")) then return 0 end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem
		qxStr = string.gsub( qjStr, "*", "0")
		if butNum > 5 or butNum < 1 or string.find(qxStr,"%D") then
			return 0
		end
		for i=1,butNum do
			sortStr = sort_str( xxItem[i] )
			if sortStr ~= nil and string.len( sortStr) == 10 then
				butStr = butStr.."00"..format_num(butPower)..sortStr.."^"
			else
				return 0
			end
		end

	--单式自选倍投 1#0522#单式号码#倍数 0522 = 2,0,0,2
	elseif butType == '2' then
		if itemLen < 1 or itemLen > 2 or string.find(qjStr,"%D") or string.len(itemList[1]) ~= 10 or sort_str(itemList[1])==nil  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		butNum = 1
		butStr = "00"..format_num( butPower )..sort_str(itemList[1]).."^"


	--复式 1#053#复式号码 053 = 2,0,0,3
	elseif butType == '3' then
		if itemLen < 1 or itemLen > 2 or string.find(qjStr,"%D") or string.len(itemList[1]) < 12 or string.len(itemList[1]) > 24 or sort_str(itemList[1])==nil  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		butNum = combination( string.len(itemList[1])/2, 5)
		butStr = "10"..format_num( butPower )..sort_str(itemList[1]).."^"


	--复试倍投 1#0532#复式号码#倍数 0532 = 2,0,0,4
	elseif butType == '4' then
		if itemLen < 1 or itemLen > 2 or string.find(qjStr,"%D") or string.len(itemList[1]) < 12 or string.len(itemList[1]) > 24 or sort_str(itemList[1])==nil  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		butNum = combination( string.len(itemList[1])/2, 5)
		butStr = "10"..format_num( butPower )..sort_str(itemList[1]).."^"

	--胆拖自选 1#056#胆号码*拖号码 056 = 2,0,0,5
	elseif butType == '5' then
		if itemLen < 1 or itemLen > 2  then
			return 0
		end

		qxStr = string.gsub(itemList[1], "*", "")
		if string.find(qxStr,"%D") or sort_str(qxStr)==nil or string.len(qxStr) < 12 then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		xItem = Split( itemList[1], "*")

		if table.getn( xItem ) ~= 2 or sort_str(xItem[1])==nil or sort_str(xItem[2])==nil or string.len(xItem[1]) >8 or string.len(xItem[1]) < 2 then
			return 0
		end

		danNum = string.len(xItem[1])/2
		tuoNum = string.len(xItem[2])/2

		butNum = combination(tuoNum, (5-danNum))
		butStr = "20"..format_num( butPower )..sort_str(xItem[1]).."*"..sort_str(xItem[2]).."^"



	else
		return 0
	end;
	return butNum,butPower,butStr,inStr
end

function query_155(butType,queryStr,djStr)
	local nBingo = {0,0,0}
	local nSeriesNum = 1
	local butPower = 1
	local itemList = {}
	local bullStr = {}
	itemList = Split( queryStr, "#" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	xxStr = string.gsub(itemList[1], "*", "x")
	xxItem = Split(xxStr, "xx")

	nSeriesNum = ComputeSeriesNum(djStr)
	--=================================单复式中奖计算=======================
	if butType == '1' or butType =='2'  or butType == '3' or butType == '4' then
		nBingo = Compute155(butType,xxStr,djStr)
	--====================================胆拖=============================
	elseif butType == '5' then
		nBingo = Compute155DT(xxStr,djStr)
	end
	for i = 1,3 do
		nBingo[i] = nBingo[i] * butPower
	end

	return nBingo[1]..","..nBingo[2]..","..nBingo[3]
end
--test
--print(check_155('1',"0102030405**0501030806"))
--print(check_155('2',"0203040506#12"))
--print(check_155('3',"020304050607"))
--print(check_155('3',"1514020304050607#2"))
--print(check_155('5',"10010203*04050607#12"))
--print(random_155('1',"5#1"))
--print(ComputeSeriesNum("0203060709"))
--print(Compute155('3',"0102030506070809","0102030507"))
--print(query_155('2',"0102030405**0102030409**0102030509#10","0102030405"))
--print(query_155('3',"0102030405060708#10","0102030405"))
--print(query_155('5',"01020305*06070809","0102030506"))
